import * as apis from '../services/applicationService'
import { routerRedux } from 'dva/router';
import { message } from 'antd'

export default {
    namespace: 'application',

    state: {
        verification: {
            technologyId: "",
            phase: 1,
            examination: -1,
            reason: "",
            verificationMechanism: {},
            verificationAgreement: {}
        },
        verificationList: [],
        test: {},
        verificationInformation: {},
        finishedInformation: {},
    },

    reducers: {
        setVerificationList: setVerificationList,
        setVerification: setVerification,
        setVerificationMechanism: setVerificationMechanism,
        setPhase: setPhase,
        setVerificationAgreement: setVerificationAgreement,
        setExamination: setExamination,
        setTest: setTest,
        setVerificationInformation: setVerificationInformation,
        setFinishedInformation: setFinishedInformation,
    },

    effects: {
        getVerificationListByUserId: getVerificationListByUserId,
        getVerificationPhaseByTechnologyId: getVerificationPhaseByTechnologyId,
        deleteVerificationByTechnologyId: deleteVerificationByTechnologyId,
        qualificationExamination: qualificationExamination,
        getTechnologyVerificationMechanismByTechnologyId: getTechnologyVerificationMechanismByTechnologyId,
        getVerificationAgreement: getVerificationAgreement,
        goToNextProcess: goToNextProcess,
        getTestMechanismInformation: getTestMechanismInformation,
        getVerificationMechanismInformation: getVerificationMechanismInformation,
        getFinishedInformation: getFinishedInformation,
        restartProcess: restartProcess,
        technologyValuation: technologyValuation
    }
}

function setFinishedInformation(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.finishedInformation = payload.data
    return _state
}

function setVerificationInformation(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verificationInformation = payload.data
    return _state
}

function setTest(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.test = payload.data
    return _state
}

function setExamination(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verification.examination = payload.data
    return _state
}

function setVerificationAgreement(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verification.verificationAgreement = payload.data
    return _state
}

function setPhase(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verification.phase = payload.data.verificationPhase
    return _state
}

function setVerificationMechanism(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verification.verificationMechanism = payload.data
    return _state
}

function setVerificationList(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verificationList = payload.data
    return _state
}

function setVerification(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.verification.technologyId = payload.data.verificationApplicationId
    _state.verification.phase = payload.data.verificationPhase
    _state.verification.examination = payload.data.examinationStatus
    _state.verification.reason = payload.data.reason
    return _state
}

/**
 * 技术估值
 */
function* technologyValuation(payload, { select, call, put }) {
    let result = yield call(apis.technologyValuation, payload.data)

    yield put({
        type: "app/setServerRes",
        data: result.data
    })
}

/**
 * 提交验证申请
 */
function* qualificationExamination(payload, { select, call, put }) {
    let result = yield call(apis.qualificationExamination, payload.data)

    if (result) {
        yield put(routerRedux.replace('/application'))
    }

    if(result.data.code === 200){
        message.success("提交成功",3)
    }

    if(result.data.code !== 200){
        message.error(result.data.msg+"!提交失败，请重新提交！")
    }

}

/**
 * 查询当前技术供应方的所有技术
 */
function* getVerificationListByUserId(payload, { select, call, put }) {
    let result = yield call(apis.getVerificationListByUserId, payload.data)
    yield put({
        type: "application/setVerificationList",
        data: result.data.data
    })
}

/**
 * 查询所选技术所到的验证阶段
 */
function* getVerificationPhaseByTechnologyId(payload, { select, call, put }) {
    let result = yield call(apis.getverificationPhaseByTechnologyId, payload.data)
    yield put({
        type: "application/setVerification",
        data: result.data.data
    })

    yield put(routerRedux.replace('/application/process'))
}

/**
 * 根据id删除技术验证信息
 */
function* deleteVerificationByTechnologyId(payload, { select, call, put }) {
    let result = yield call(apis.deleteVerificationByTechnologyId, payload.data)

}

/**
 * 查询当前技术申请所分配的验证机构
 */
function* getTechnologyVerificationMechanismByTechnologyId(payload, { select, call, put }) {
    let result = yield call(apis.getTechnologyVerificationMechanismByTechnologyId, payload.data)

    yield put({
        type: "application/setVerificationMechanism",
        data: result.data.data
    })
}

/**
 * 流程推进到下一个阶段（无需审核）
 */
function* goToNextProcess(payload, { select, call, put }) {
    let result = yield call(apis.goToNextProcess, payload.data)

    yield put({
        type: "application/setPhase",
        data: result.data.data
    })
}

/**
 * 查询当前技术的验证协议相关信息
 */
function* getVerificationAgreement(payload, { select, call, put }) {
    let result = yield call(apis.getVerificationAgreement, payload.data)

    yield put({
        type: "application/setVerificationAgreement",
        data: result.data.data
    })
}

/**
 * 查询当前技术的测试数据相关信息
 */
function* getTestMechanismInformation(payload, { select, call, put }) {
    let result = yield call(apis.getTestMechanismInformation, payload.data)

    yield put({
        type: "application/setTest",
        data: result.data.data
    })
}

/**
 * 查询当前技术的验证机构相关信息
 */
function* getVerificationMechanismInformation(payload, { select, call, put }) {
    let result = yield call(apis.getVerificationMechanismInformation, payload.data)
    yield put({
        type: "application/setVerificationInformation",
        data: result.data.data
    })
}

/**
 * 获取完成页面相关信息
 */
function* getFinishedInformation(payload, { select, call, put }) {
    let result = yield call(apis.getFinishedInformation, payload.data)
    yield put({
        type: "application/setFinishedInformation",
        data: result.data.data
    })
}

/**
 * 重新验证
 */
function* restartProcess(payload, { select, call, put }) {
    let result = yield call(apis.restartProcess, payload.data)
    let { verification } = yield select(state => state.application)
    yield put({
        type: "application/getverificationPhaseByTechnologyId",
        data: {
            id: verification.technologyId
        }
    })
}